<template>
  <div>
    <h1>创建文章</h1>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm123"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="封面" prop="poster">
        <el-input v-model="ruleForm.poster"></el-input>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="ruleForm.author"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择活动区域">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详情" prop="detail">
        <el-input type="textarea" v-model="ruleForm.detail"></el-input>
        <quill-editor
          v-model="ruleForm.detail"
          ref="myQuillEditor"
          :options="editorOption"
          @blur="onEditorBlur($event)"
          @focus="onEditorFocus($event)"
          @ready="onEditorReady($event)"
        >
        </quill-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">立即创建</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: "<h2>I am Example</h2>",
      editorOption: {
        // some quill options
      },
      ruleForm: {
        title: "",
        description: "",
        poster: "",
        type: "",
        author: "",
        detail: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 5,
            max: 25,
            message: "长度在 5 到 25 个字符",
            trigger: "change",
          },
        ],
        description: [
          { required: true, message: "请输入描述信息", trigger: "change" },
        ],
        poster: [
          {
            type: "url",
            required: true,
            message: "请输入封面图地址",
            trigger: "change",
          },
        ],
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
        author: [{ required: true, message: "请输入作者", trigger: "change" }],
        detail: [{ required: true, message: "请填写详情", trigger: "blur" }],
      },
    };
  },
  methods: {
    onEditorBlur(editor) {
      console.log("editor blur!", editor);
    },
    onEditorFocus(editor) {
      console.log("editor focus!", editor);
    },
    onEditorReady(editor) {
      console.log("editor ready!", editor);
    },
    submitForm() {
      // console.log(this.$refs)
      this.$refs.ruleForm123.validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
          // ajax
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.ruleForm123.resetFields();
    },
  },
};
</script>

<style></style>
